package org.makcro.sms.services.chat.web.client.mvc.view;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.client.Constants;
import org.makcro.sms.services.chat.web.client.services.message.BulkServiceAsync;
import org.makcro.sms.services.chat.web.client.widgets.bulk.BulkMessagesSearchTabItem;
import org.makcro.sms.services.chat.web.client.widgets.bulk.BulkSearchPanel;
import org.makcro.sms.services.chat.web.client.widgets.main.MainTabPanel;
import org.makcro.sms.services.chat.web.shared.dto.BulkMessageDto;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * View class for the bulk tab item.
 * 
 * @author makcro
 * 
 */
public class BulkTabView extends View {

	// bulk tab item instance
	private BulkMessagesSearchTabItem	bulkTabItem;
	private BulkSearchPanel				bulkSearchPanel;

	public BulkTabView(Controller controller) {
		super( controller );
	}

	/**
	 * Initializes this view
	 */
	@Override
	protected void initialize() {
		super.initialize();
	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType eventType = event.getType();

		if (eventType.equals( AppEvents.BulkEvents.BULK_TAB_REQUESTED )) {
			onBulkTabRequested( event );
		}

	}

	/**
	 * Gets reference to main tab panel and adds bulk tab item to it.
	 * 
	 * @param event
	 */
	private void onBulkTabRequested(AppEvent event) {
		MainTabPanel mainTabPanel = Registry.get( Constants.Ui.MAIN_TAB_PANEL );
		BulkMessagesSearchTabItem tabItem = getBulkTabItem();
		mainTabPanel.add( tabItem );
		mainTabPanel.setSelection( tabItem );
	}

	public BulkSearchPanel getBulkSearchPanel() {
		if (this.bulkSearchPanel == null) {
			// get bulk service
			final BulkServiceAsync bulkService = Registry.<BulkServiceAsync> get( Constants.Services.BULK_SERVICE );
			// create rpc proxy
			RpcProxy<PagingLoadResult<BulkMessageDto>> rpcProxy = new RpcProxy<PagingLoadResult<BulkMessageDto>>() {

				@Override
				protected void load(Object loadConfig, AsyncCallback<PagingLoadResult<BulkMessageDto>> callback) {
					bulkService.search( (PagingLoadConfig) loadConfig, callback );
				}
			};

			// init loader
			PagingLoader<PagingLoadResult<BulkMessageDto>> pagingLoader = new BasePagingLoader<PagingLoadResult<BulkMessageDto>>( rpcProxy );
			this.bulkSearchPanel = new BulkSearchPanel( pagingLoader );
			this.bulkSearchPanel.setHeading( "Pretraga bulk poruka" );
		}
		return bulkSearchPanel;
	}

	/**
	 * Getter for tab item. Initializes it and adds search panel to it.
	 * @return
	 */
	public BulkMessagesSearchTabItem getBulkTabItem() {
		if (this.bulkTabItem == null) {
			this.bulkTabItem = new BulkMessagesSearchTabItem();
			this.bulkTabItem.setLayout( new FitLayout() );
			this.bulkTabItem.add( getBulkSearchPanel() );
		}
		return bulkTabItem;
	}
}
